package org.elkastud;

import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.elkastud.fasta.FastaSequence;
import org.jdesktop.application.Action;

/**
 *
 * @author Mariusz
 */
public class StartDialog extends javax.swing.JDialog {

    /** Creates new form StartDialog */
    public StartDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        initSeqList();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        methodChoose = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        seqList = new javax.swing.JList();
        buttonNeedleman = new javax.swing.JRadioButton();
        buttonSmith = new javax.swing.JRadioButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        seqArea = new javax.swing.JTextArea();
        cancalButton = new javax.swing.JButton();
        startButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        smithWatermanOptions = new javax.swing.JPanel();
        smithWatermanThreshold = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        seqList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        seqList.setName("seqList"); // NOI18N
        jScrollPane1.setViewportView(seqList);

        methodChoose.add(buttonNeedleman);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.elkastud.SeqCompApp.class).getContext().getResourceMap(StartDialog.class);
        buttonNeedleman.setText(resourceMap.getString("buttonNeedleman.text")); // NOI18N
        buttonNeedleman.setName("buttonNeedleman"); // NOI18N

        methodChoose.add(buttonSmith);
        buttonSmith.setText(resourceMap.getString("buttonSmith.text")); // NOI18N
        buttonSmith.setName("buttonSmith"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        seqArea.setColumns(20);
        seqArea.setRows(5);
        seqArea.setName("seqArea"); // NOI18N
        jScrollPane2.setViewportView(seqArea);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(org.elkastud.SeqCompApp.class).getContext().getActionMap(StartDialog.class, this);
        cancalButton.setAction(actionMap.get("cancel")); // NOI18N
        cancalButton.setText(resourceMap.getString("cancalButton.text")); // NOI18N
        cancalButton.setName("cancalButton"); // NOI18N

        startButton.setAction(actionMap.get("start")); // NOI18N
        startButton.setText(resourceMap.getString("startButton.text")); // NOI18N
        startButton.setName("startButton"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        smithWatermanOptions.setName("smithWatermanOptions"); // NOI18N

        smithWatermanThreshold.setModel(new javax.swing.SpinnerNumberModel(20, 10, 1000, 1));
        smithWatermanThreshold.setName("smithWatermanThreshold"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setToolTipText(resourceMap.getString("jLabel1.toolTipText")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout smithWatermanOptionsLayout = new javax.swing.GroupLayout(smithWatermanOptions);
        smithWatermanOptions.setLayout(smithWatermanOptionsLayout);
        smithWatermanOptionsLayout.setHorizontalGroup(
            smithWatermanOptionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(smithWatermanOptionsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(smithWatermanThreshold, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        smithWatermanOptionsLayout.setVerticalGroup(
            smithWatermanOptionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(smithWatermanOptionsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(smithWatermanOptionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(smithWatermanThreshold, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(129, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(smithWatermanOptions, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(smithWatermanOptions, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(cancalButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(startButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonNeedleman, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonSmith, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonNeedleman)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(buttonSmith)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(startButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancalButton)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                StartDialog dialog = new StartDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void cancel() {
        this.dispose();
    }

    @Action
    public void start() {
        int[] indexes = this.seqList.getSelectedIndices();
        if (indexes.length != ELEMENTS_TO_COMPARE) {
            JOptionPane.showMessageDialog(this, INCORECT_NUMBERS, TITLE, JOptionPane.ERROR_MESSAGE);
            return;
        }
        FastaSequence[] sequences = new FastaSequence[ELEMENTS_TO_COMPARE];
        for (int i=0; i<ELEMENTS_TO_COMPARE; ++i) {
            sequences[i] = (FastaSequence)this.seqList.getModel().getElementAt(indexes[i]);
        }
        if (this.buttonSmith.isSelected()) {
            SeqCompApp.getApplication().setMethod(Method.SMITH_WATERMAN);
        } else {
            SeqCompApp.getApplication().setMethod(Method.NEEDLEMAN_WUNCH);
        }
        this.dispose();
        int threshold = Integer.parseInt(this.smithWatermanThreshold.getValue().toString());
        SeqCompApp.getApplication().setSmithThreshold(threshold);
        SeqCompApp.getApplication().startCompare(sequences[0], sequences[1], sequences[2]);
    }

    private void initSeqList() {
        FastaSequence[] data = SeqCompApp.getApplication().getSequences().toArray(new FastaSequence[0]);
        this.seqList.setListData(data);
        this.seqList.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                FastaSequence seq = (FastaSequence)seqList.getModel().getElementAt(e.getFirstIndex());
                seqArea.setText(seq.getSequence());
            }
        });

        if (Method.NEEDLEMAN_WUNCH.equals(SeqCompApp.getApplication().getMethod())) {
            this.buttonNeedleman.setSelected(true);
            this.buttonSmith.setSelected(false);
            this.smithWatermanOptions.setVisible(false);
        } else {
            this.buttonSmith.setSelected(true);
            this.buttonNeedleman.setSelected(false);
            this.smithWatermanOptions.setVisible(true);
        }
        this.buttonSmith.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                if (buttonSmith.isSelected()) {
                    smithWatermanOptions.setVisible(true);
                } else {
                    smithWatermanOptions.setVisible(false);
                }
            }
        });
        int threshold = SeqCompApp.getApplication().getSmithThreshold();
        if (threshold != 0) {
            this.smithWatermanThreshold.setValue(threshold);
        }
    }

    private static final int ELEMENTS_TO_COMPARE = 3;   // liczba elementów do porównywania
    private static final String INCORECT_NUMBERS = "Elements number is incorrect. Please choose " + ELEMENTS_TO_COMPARE + " elemens.";
    private static final String TITLE = "Error";

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton buttonNeedleman;
    private javax.swing.JRadioButton buttonSmith;
    private javax.swing.JButton cancalButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.ButtonGroup methodChoose;
    private javax.swing.JTextArea seqArea;
    private javax.swing.JList seqList;
    private javax.swing.JPanel smithWatermanOptions;
    private javax.swing.JSpinner smithWatermanThreshold;
    private javax.swing.JButton startButton;
    // End of variables declaration//GEN-END:variables

}
